﻿import flash.display.BitmapData;
import mx.utils.Delegate;

dynamic class pictureBox extends MovieClip {
	public static var M_PROGRESS:String="progress";
	public static var TRACE:Boolean = true;
	//--------------------------------------------------------------------------------------
	//																					private
	private var $root :MovieClip ;
	private var $width:Number=100;
	private var $height:Number=100;
	private var $WHB:Number;
	private var $scal="fix"
	private var $picurl:String;
	private var $piclink:String;
	private var $autoload:Boolean=false;
	private var $timeout:Number=200;
	private var $nowTimeout:Number=0;
	private var _say:Say;
	private var $smooth:Boolean = false;
	private var $oldSize:Object;
	//--------------------------------------------------------------------------------------
	//																					public
	
	public function get _visible():Boolean {
		return this.$root.___pic._visible;
	}
	public function set _visible(value:Boolean) {
		this.$root.___pic._visible = value;
	}
	
	public function get say():Say{
		return this._say;
	}
	public function get Width():Number{
		return this.$width;
	}
	public function set Width(value:Number){
		this.$width=value;
		this.$WHB=this.$width/this.$height;
		if(this.$autoload){
			this.setImageScal();
		}
	}
	public function get Height():Number{
		return this.$height;
	}
	public function set Height(value:Number){
		this.$height=value;
		this.$WHB=this.$width/this.$height;
		if(this.$autoload){
			this.setImageScal();
		}
	}
	public function get TimeOut():Number{
		return this.$timeout;
	}
	public function set TimeOut(value:Number){
		this.$timeout=value;
	}
	public function get PicUrl():String{
		return this.$picurl;
	}
	public function set PicUrl(value:String){
		this.$piclink="";
		this.$picurl=value;
		if(this.$autoload){
			this.load();
		}
	}
	
	public function get PicLink():String{
		return this.$piclink;
	}
	public function set PicLink(value:String){
		this.$picurl="";
		this.$piclink=value;
		if(this.$autoload){
			this.load();
		}
	}
	
	public function get AutoLoad(){
		return this.$autoload;
	}
	public function set AutoLoad(value:Boolean){
		this.$autoload=value;
		if(this.$autoload==true){
			if(this.$piclink!="" || this.$picurl!=""){
				this.load();
			}
		}
	}
	
	public function get Smooth():Boolean {
		return this.$smooth;
	}
	public function set Smooth(value:Boolean) {
		this.$smooth = value;
		this.$root.___pic.forceSmoothing = value;
	}
	//--------------------------------------------------------------------------------------
	//																					function 
	
	private function checkScalModle():Boolean{
		if (this.$scal != "none" && this.$scal != "width" && this.$scal != "height" && this.$scal != "both" && this.$scal != "fix") {
			this._say.wsay("scal not right!["+this.$scal+"] set to Default: fix");
			this.$scal = "fix";
			return false;
		}else{
			return true;
		}
	}
	public function get Scal():String{
		return this.$scal;
	}
	public function set Scal(value:String){
		this.$scal=value;
		this.checkScalModle();
	}
	public function pictureBox(root:MovieClip){
		this.initsay();
		if(root==undefined){
			root=this;
		}
		this.$root=root;
		this.$WHB=this.$width/this.$height;
		this._xscale = this._yscale=100;
		
		this.initBroadCaster();
		//this._say.msay("initOK");
	}
	private function initBroadCaster() {
		AsBroadcaster.initialize(this);
	}
	/**
	 * load picture
	 */
	public function load() {
		if (this.$picurl == "" && this.$piclink == "") {
			return;
		}
		if (this.$piclink != "") {
			this.linkPic();
		} else {
			this.loadPic();
		}
	}
	public function unload() {
		//	trace("unloaded pics")
		this.$root.___pic.removeMovieClip();
		this.$root.loadtrackmc.removeMovieClip(); 
	}
	
	private function loadPic(){
		this.$root.___pic.removeMovieClip();
		this.$root.loadtrackmc.removeMovieClip(); 
		this.$root.createEmptyMovieClip("___pic",10);
		this.$root.___pic.loadMovie(this.$picurl);
		this.$root.createEmptyMovieClip("loadtrackmc",5);
		this.$root.loadtrackmc.onEnterFrame=Delegate.create(this,this.loadTrack);
		this.$nowTimeout=0;
	}
	private function loadTrack(){
		//this.$root.___pic._visible=false;
		var ld:Number=this.$root.___pic.getBytesLoaded();
		var tt:Number=this.$root.___pic.getBytesTotal();
		var ldf:Number=this.$root.___pic._framesloaded;
		var ttf:Number=this.$root.___pic._totalframes;
		var picw:Number=this.$root.___pic._width;
		
		if(tt<=0){
			if(ttf>0 && ld==tt && tt>0 && ttf==ldf && ldf>=1 && picw>0.5){
				this.setImageScal();
				this.broadcastMessage(M_PROGRESS,ldf,ttf);
				delete this.$root.loadtrackmc.onEnterFrame;
				this.$root.loadtrackmc.removeMovieClip();
			}else{
				//
				if(this.$nowTimeout++>this.$timeout){
					this._say.esay("load picture timeout!["+this.PicLink+this.PicUrl+"]");
					this.$nowTimeout=0;
					this.broadcastMessage(M_PROGRESS,-1,-1);
					delete this.$root.loadtrackmc.onEnterFrame;
					this.$root.loadtrackmc.removeMovieClip();
				}else{
					//this._say.wsay("没开始,或出错等待...["+ld+"/"+tt+"]");
				}
				
			}
		}else{
			if(ld<tt){
				this.broadcastMessage(M_PROGRESS,ld,tt);
			}else if(ld==tt && ttf>0){
				this.setImageScal();
				this.broadcastMessage(M_PROGRESS,ld,tt);
				delete this.$root.loadtrackmc.onEnterFrame;
				this.$root.loadtrackmc.removeMovieClip();
			}
		}
	}
	private function linkPic() {
		this.$root.___pic.removeMovieClip();
		this.$root.attachMovie(this.$piclink, "___pic", 10);
		
		this.setImageScal();
		this.broadcastMessage(M_PROGRESS, 100, 100);
	}

	public function draw(withScale:Boolean):BitmapData {
		if (withScale == undefined) {
			withScale = false;
		}
		if (withScale) {
			var bmp:BitmapData = new BitmapData(this.Width, this.Height, true, 0x0);
			bmp.draw(this.$root.___pic);
			return bmp;
		}else{
			var s = this.$scal;
			this.Scal = "none";
			this.setImageScal();
			var bmp:BitmapData = new BitmapData(this.$root.___pic._width, this.$root.___pic._height, true, 0x0);
			bmp.draw(this.$root.___pic);
			this.Scal = s;
			this.setImageScal();
			return bmp;
			
		}
	}
	
	private function setImageScal() {
		this.$root.___pic._xscale=this.$root.___pic._yscale=100;
		this.$root.___pic._visible=true;
		this.$root.___pic.cacheAsBitmap = true;
		this.$root.___pic.forceSmoothing = this.$smooth;
		if (this.$scal == "none") {
			this.$root.___pic._x = this.$root.___pic._y = 0;
			return;
		} else if (this.$scal == "width") {
			var wb:Number = this.$width/this.$root.___pic._width;
			this.$root.___pic._width = this.$width;
			this.$root.___pic._height = this.$root.___pic._height*wb;
		} else if (this.$scal == "height") {
			var hb:Number = this.$height/this.$root.___pic._height;
			this.$root.___pic._height = this.$height;
			this.$root.___pic._width = this.$root.___pic._width*hb;
		} else if (this.$scal == "both") {
			this.$root.___pic._height = this.$height;
			this.$root.___pic._width = this.$width;
		} else if (this.$scal="fix") {
			var whb2:Number = this.$root.___pic._width/this.$root.___pic._height;
			if (this.$WHB>whb2) {
				//h
				var hb:Number = this.$height/this.$root.___pic._height;
				this.$root.___pic._height = this.$height;
				this.$root.___pic._width = this.$root.___pic._width*hb;
			} else {
				//w
				var wb:Number = this.$width/this.$root.___pic._width;
				this.$root.___pic._width = this.$width;
				this.$root.___pic._height = this.$root.___pic._height*wb;
			}
		} else {
			return;
		}
		//trace(this.$width+"||"+this.$height);
		this.$root.___pic._x=(this.$width-this.$root.___pic._width)/2;
		this.$root.___pic._y=(this.$height-this.$root.___pic._height)/2;
	}
	
	private function initsay(){
		this._say=new Say("pic box:");
		this._say.addListener(this);
		this[Say.M_SAY]=this.sayfun;
	}
	private function sayfun(o){
		if(TRACE){
			trace(o);
		}
	}
}